/**
 * 
 */
package top.zyblogs.spider.utils;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.fileUpload;

import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.ibatis.annotations.Case;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import top.zyblogs.spider.film.dao.FilmMapper;
import top.zyblogs.spider.film.entity.Film;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;

/**
 * @author zhaoyibo
 *
 */
@Repository
public class SpiderUtil implements PageProcessor {
	static Map<String, String> filmIds=new HashMap<>();
	private static final String IMDBURL="http://www.imdb.com/title/";
	private static final String ERRORTEXT="呃...你想访问的页面不存在";
	@Autowired(required=false)
	private FilmMapper filmMapper;
	
	@Autowired(required=false)
	private MovieUtil movieUtil;
	private Site site =Site.me().setRetryTimes(3).setSleepTime(100);
	
	@Override
	public void process(Page page) {
		// TODO Auto-generated method stub
		Html html=page.getHtml();
		
		for (int j = 1; j <= 25; j++) {
			String pre="//*[@id=\"content\"]/div/div[1]/ol/li["+j+"]/";
			Film film=new Film();
			String filmId=StringUtil.getUUID();
			film.setId(filmId);
			film.setFilmName(html.xpath(pre+"div/div[2]/div[1]/a/span[1]/text()").toString());//电影名字
			filmIds.put(film.getFilmName(),filmId);
			
			film.setPostImg(html.xpath(pre+"div/div[1]/a/img/@src").get());//海报的地址
			film.setScore(Double.parseDouble(html.xpath(pre+"div/div[2]/div[2]/div/span[2]/text()").get()));//得分
			film.setDoubanUrl(html.xpath(pre+"div/div[1]/a/@href").get());
			
			String[] strs=html.xpath(pre+"div/div[2]/div[2]/p[1]/text()").toString().split("/");
			for (int i = 0; i < strs.length; i++) {
				if (i==0) {
					String[] strings=strs[i].split(":");
					Spider.create(movieUtil).addUrl(film.getDoubanUrl()).thread(5).run();
				}
			}
			updateFilm(film);
		}
	}
 	@Override
	public Site getSite() {
		// TODO Auto-generated method stub
		return site;
	}
	
	/**
	 * @Description:
	 * @author zhaoyibo  
	 * @date 2018年11月21日  上午11:42:40
	 */
	private void updateFilm(Film film) {
		try {
			filmMapper.updateFilm(film);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
}
